package jsonlistview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.example.administrator.myapplication.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import app.Application;
import httptool.getHttpUrl;
import httptool.takeUrlResource;

/**
 * Created by cch on 2016/6/6.
 */

class MyAdapter extends BaseAdapter{
    private Context  context;
    private ArrayList<Map<String,String>> data;
    public MyAdapter(Context context,ArrayList<Map<String,String>> data) {
            this.context = context;
            this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            holder  = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
            holder.iv = (ImageView) convertView.findViewById(R.id.iv);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.desrciption = (TextView) convertView.findViewById(R.id.description);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        String title = data.get(position).get("title");
        String  dec   = data.get(position).get("dec");
        String  iv  = data.get(position).get("img");

        Application.universalImageLoader.displayImage(iv,holder.iv,Application.options);
        holder.title.setText(title);
        holder.desrciption.setText(dec);
        return convertView;
    }
    class ViewHolder{
        private ImageView iv;
        private TextView title,desrciption;
    }
}


public class JsonActivity extends Activity {
    private ListView lv;
    private MyAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.main);
        lv = (ListView) findViewById(R.id.listview);
        getJson2();
    }

    private ArrayList<Map<String,String>> data = new ArrayList<Map<String,String>>();
    public void getJson2() {
        takeUrlResource.getJson(getApplicationContext(), getHttpUrl.index, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e("youmeiyou ",s);
                try {
                    JSONObject jso = new JSONObject(s);
                    String retcode = jso.getString("retcode");
                    if (retcode.equals("200")){
                        JSONArray jsa = jso.getJSONArray("news");
                        for(int i = 0;i < jsa.length();i++){
                            String title = jsa.getJSONObject(i).getString("title");
                            String writer = jsa.getJSONObject(i).getString("writer");
                            Log.e("z这个是", title + "---->" + writer);
                            Map<String,String> map = new HashMap<String,String>();
                            map.put("title",title);
                            map.put("dec", writer);
                            JSONArray img = jsa.getJSONObject(i).getJSONArray("imgs");
                            for (int j = 0;j < img.length();j++){
                                map.put("img",img.getString(j));
                            }
                            data.add(map);
                        }
                        Log.e("这个是什么东西",data.size()+"132132");
                        adapter = new MyAdapter(getApplicationContext(),data);
                        lv.setAdapter(adapter);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}


